package com.tin.restfinder;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;



public class tag extends ListActivity {
        private EditText filterText = null;
        ArrayAdapter<String> adapter = null;
        int where = 0;
       
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
       
            setContentView(R.layout.tag);
       
            filterText = (EditText) findViewById(R.id.search_box);
            filterText.addTextChangedListener(filterTextWatcher);
              
            setListAdapter(adapter = new ArrayAdapter<String>(this, R.layout.simple_list_item_1, Data.conf.getTags())); //wyszukiwanie tagow
      //      final int where = 0;
            final ListView list = getListView();
            list.setTextFilterEnabled(true);

             list.setOnItemClickListener(
                     new OnItemClickListener(){
                      public void onItemClick(AdapterView<?> parent, View view,
                                  int position, long id) {
                         
                    	  

                    	 if (where == 1) {
                    		setListAdapter(adapter = new ArrayAdapter<String>
                    		 	(tag.this, R.layout.simple_list_item_1, Data.conf.getTags()));
                      		Data.SetRestDetailsByName(((TextView) view).getText().toString());
                    		where =0;
                    		Intent intent2 = new Intent(tag.this, RestDetails.class);
                            startActivity(intent2);
                            
                    		
                    	 }
                    		 	
                    	 else 	{
                    		Data.user.SearchByTags(((TextView) view).getText().toString());
                    		setListAdapter(adapter = new ArrayAdapter<String>
                    			(tag.this, R.layout.simple_list_item_1, Data.user.getChosen_rest()));
                    		where = 1;
                    	}
                        }
                          });
       
        }
       
        private TextWatcher filterTextWatcher = new TextWatcher() {
       
            public void afterTextChanged(Editable s) {
            }
       
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }
       
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                adapter.getFilter().filter(s);
            }
       
        };
       
       

	@Override
	protected void onDestroy() {
	    super.onDestroy();
	    filterText.removeTextChangedListener(filterTextWatcher);
	};
	

@Override
public void onBackPressed() {
	if (where == 1) {
		setListAdapter(adapter = new ArrayAdapter<String>
		(tag.this, R.layout.simple_list_item_1, Data.conf.getTags()));
		where = 0;
	}	
}
@Override
protected void onResume() {
	super.onResume();
	setListAdapter(adapter = new ArrayAdapter<String>
	(tag.this, R.layout.simple_list_item_1, Data.conf.getTags()));
}

}


